﻿using CJiaBao.Core.Utils;
using Core.Configuration.Api;
using Core.Configuration.Config;
using Core.Quartz;
using Microsoft.AspNetCore.Mvc;

namespace IconYuan.Api.Controllers.Systems
{
    /// <summary>
    /// 定时任务
    /// </summary>
    public class QuartzController : ApiBase
    {
        readonly QuartzPro quartz;
        readonly string groupName = "UrlGroup";
        public QuartzController(QuartzPro quartz)
        {
            this.quartz = quartz;
        }

        /// <summary>
        /// 开始加载环境
        /// </summary>
        [HttpGet]
        public async void Start()
        {
            await quartz.Start();
        }

        /// <summary>
        /// 添加Job
        /// </summary>
        /// <param name="secends"></param>
        [HttpGet]
        public async void AddJob(int secends = 1800)
        {
            //await quartz.CreateJob<UrlJob>("job1", groupName, " 0/3 * * * * ? ");
            var url = $"{GlobalConfig.ApiDomain}system/TestJob";
            await quartz.CreateJob<UrlJob>(new UrlJobInfo()
            {
                Seconds = secends,
                Url = url,
                JobName = SecureHelper.MD5(url),
                GroupName = groupName
            });
        }

        /// <summary>
        /// 暂停Job
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> PauseJob(string jobName)
        {
            await quartz.PauseJob("job1", groupName);
            return true;
        }

        /// <summary>
        /// 恢复Job
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> ResumeJob(string jobName)
        {
            await quartz.ResumeJob(jobName, groupName);
            return true;
        }
       
    }
}
